package com.peanut.devlibrary.util;

import android.content.Context;

import org.json.JSONObject;

import cn.smssdk.EventHandler;
import cn.smssdk.SMSSDK;

/**
 * Created by anonymouslystudio on 2016/7/27.
 */
public class SmsUtil {
    private static final String TAG = SmsUtil.class.getSimpleName();
    private Context ctx;
    private CustomEventHandler handler;
    private OnSmsStatusChangeListener listener;
    private static SmsUtil _instance;
    private SmsUtil(){}
    public static SmsUtil getInstance(){
        if(_instance == null){
            _instance = new SmsUtil();
        }
        return _instance;
    }

    /**
     * initialize context
     * @param ctx
     */
    public void init(Context ctx){
        this.ctx = ctx;

    }

    public void registReceiver(){
        handler = new CustomEventHandler();
        SMSSDK.registerEventHandler(handler);
    }

    /**
     * set sms event listener
     * @param lis
     */
    public void setOnSmsStatusChangeListener(OnSmsStatusChangeListener lis){
        this.listener = lis;
    }

    public boolean hasListener(){
        return listener != null;
    }

    /**
     * release resources
     */
    public void removeReceiver(){
        SMSSDK.unregisterAllEventHandler();
        handler = null;

    }


    /**
     * request sms code from server
     */
    public void getVCode(String targetPhone){
        SMSSDK.getVerificationCode("86",targetPhone);
    }

    /**
     * check if vcode is validate
     */
    public void checkVCode(String phone, String code){
        SMSSDK.submitVerificationCode("86",phone,code);
    }

    class CustomEventHandler extends EventHandler{

        @Override
        public void afterEvent(int event, int result, Object o) {
            if ( result == SMSSDK.RESULT_COMPLETE ) {
                //回调完成
                if ( event == SMSSDK.EVENT_GET_VERIFICATION_CODE ) {
                    //获取验证码成功

                    if(listener != null){
                        listener.onRequestSmsFinished(true,"");
                    }
                } else if ( event == SMSSDK.EVENT_GET_SUPPORTED_COUNTRIES ) {
                    //返回支持发送验证码的国家列表
                }
            } else if(result == SMSSDK.RESULT_ERROR){
                String error = o.toString();
                // 根据服务器返回的网络错误，给toast提示
                try {
                    Throwable throwable = (Throwable) o;
                    throwable.printStackTrace();
                    JSONObject object = new JSONObject(throwable.getMessage());
                    String des = object.optString("detail");//错误描述
                    int status = object.optInt("status");//错误代码
                    if (status > 0 && !StringUtil.isNullOrEmpty(des)) {
                        error = des;
                    }
                } catch (Exception e) {
                    //do something
                }
                if ( event == SMSSDK.EVENT_GET_VERIFICATION_CODE ) {
                    //获取验证码成功
                    if(listener != null){
                        listener.onRequestSmsFinished(false, error);
                    }
                } else if ( event == SMSSDK.EVENT_GET_SUPPORTED_COUNTRIES ) {
                    //返回支持发送验证码的国家列表
                }
            }else {
                ((Throwable) o).printStackTrace();
            }
        }
    }

    /**
     * mob sms request changed callback
     */
    public interface OnSmsStatusChangeListener{
        void onRequestSmsFinished(boolean success, String failReason);
    }

}
